package myJDBC.lowLevel;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import myJDBC.*;

public class AttributeTypes {
			// {attr_Type_id, attr_type_name}
			public static ArrayList<String> returnAttrTypes() {
				ArrayList<String> al = new ArrayList<String>();
				String selectAttrTypes = "select t1.\"attr_type_id\", t1.\"name\" from \"attr_type\" t1 ";
				if ( myConnection.dbConnection != null) {
					try {
						Statement statement = myConnection.dbConnection.createStatement();
						ResultSet rs = statement.executeQuery(selectAttrTypes);
						String tmp;
						while (rs.next()) {
							tmp = rs.getString(1);
							al.add(tmp);
							tmp = rs.getString(2);
							al.add(tmp);
						}
					} catch (SQLException e) {
						System.out.println(e.getMessage());
					}
					return al;
				} else
					return null;
			}
			
			//attr_type_name
			public static String returnAttrType(int attrTypeId) {
				String selectAttrType = "select t1.\"name\" from \"attr_type\" t1 where t1.\"attr_type_id\"=? ";
				String tmp = null;
				if (myConnection.dbConnection != null) {
				 	 try (PreparedStatement pstmt = myConnection.dbConnection
				                .prepareStatement(selectAttrType)) {
						   pstmt.setInt(1, attrTypeId);
						   pstmt.executeUpdate(); 
						   ResultSet rs = pstmt.executeQuery();			
						while (rs.next()) {
							tmp = rs.getString(1);
						}
					} catch (SQLException e) {
						System.out.println(e.getMessage());
					}
					return tmp;
				} else
					return null;
			}
			
			//attr_type_id
			public static Integer returnAttrTypeWithId(String name) {
				String selectAttrType = "select t1.\"attr_type_id\" from \"attr_type\" t1 where t1.\"name\"=? ";
				Integer tmp = null;
				if (myConnection.dbConnection != null) {
				 	 try (PreparedStatement pstmt = myConnection.dbConnection
				                .prepareStatement(selectAttrType)) {
						   pstmt.setString(1, name);
						   pstmt.executeUpdate(); 
						   ResultSet rs = pstmt.executeQuery();					
						while (rs.next()) {
							tmp = rs.getInt(1);
						}
					} catch (SQLException e) {
						System.out.println(e.getMessage());
					}
					return tmp;
				} else
					return null;
			}
}
